package com.henu.ws.rest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import org.smart4j.framework.ioc.annotation.Inject;
import org.smart4j.framework.tx.annotation.Service;

import com.henu.model.Category;
import com.henu.service.AppraiseService;
import com.henu.service.CategoryService;
import com.henu.service.ProductInfoService;
import com.smart.plugin.ws.WebService;

@Service
@WebService(value = "/Product", type = WebService.Type.REST)
@Produces(MediaType.APPLICATION_JSON)
public class ProductInfoWS {
    @Inject
    private ProductInfoService productInfoService;
    @Inject
    private CategoryService categoryService;

    @Inject
    private AppraiseService appraiseService;

    @POST
    @Path("/GetSearchProduct")
    public Map queryProductByKey(@FormParam("keyword") String key) {
        Map m = new HashMap();
        System.out.println(key);
        m.put("GetProductBySearchResult", productInfoService.findByKey(key));
        System.out.println(m.toString());
        return m;
    }

    @GET
    @Path("/GetSearchProduct1")
    public Map queryProductByKey1(@QueryParam("keyword") String key) {
        Map m = new HashMap();
        System.out.println(key);
        m.put("GetProductBySearchResult", productInfoService.findByKey(key));
        System.out.println(m.toString());
        return m;
    }

    @GET
    @Path("/GetProductList")
    public Object queryProductByCategory(@QueryParam("categoryid") int categoryid,
            @QueryParam("productid") int productid) {
        if (productid > 0) {
            return productInfoService.findByProductIdMore(productid);
        }
        return productInfoService.findByCategory(categoryid);
    }

    @GET
    @Path("/GetCategory")
    public List<Category> getProductList() {
        return categoryService.getCategorys();
    }

    @GET
    @Path("/GetProduct")
    public Object getProductByID(@QueryParam("id") int productid) {
        return productInfoService.findByProductId(productid);
    }

    @GET
    @Path("/GetAppraise")
    public Object getAppraise(@QueryParam("id") int productid) {
        return appraiseService.getAppraise(productid);
    }

    @GET
    @Path("/GetStar")
    public List getStar() {
        return productInfoService.getStar();
    }
}
